package it.polimi.traveldream.entity;

import it.polimi.traveldream.dto.PaidDTO;

import java.io.Serializable;

import javax.persistence.EmbeddedId;
import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.JoinColumns;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;

/**
 * The persistent class for the paid database table.
 * 
 */
@Entity
@Table(name = "paid")
@NamedQuery(name = "Paid.findAll", query = "SELECT p FROM Paid p")
public class Paid implements Serializable {
	private static final long serialVersionUID = 1L;

	@EmbeddedId
	private PaidPK id;

	@NotNull
	@Min(0)
	private Integer quantity;

	@ManyToOne
	@JoinColumn(name = "product", nullable = false)
	private Product product;

	@ManyToOne
	@JoinColumns({
			@JoinColumn(name = "cartpackage", referencedColumnName = "cartpackage", nullable = false, insertable = true, updatable = false),
			@JoinColumn(name = "user", referencedColumnName = "user", nullable = false, insertable = true, updatable = false) })
	private GiftlistElement giftlistElement;

	public Paid() {
		this.setId(new PaidPK());
	}

	public Paid(Product product, int quantity) {
		this();
		this.setProduct(product);
		this.setQuantity(quantity);
	}

	public Paid(PaidDTO paid) {
		PaidPK paidPK = new PaidPK();
		paidPK.setCartpackage(paid.getCartpackageId());
		paidPK.setProduct(paid.getProduct().getId());
		paidPK.setUser(paid.getUserId());
		this.setId(paidPK);
		this.setProduct(new Product(paid.getProduct()));
		this.setQuantity(paid.getQuantity());
	}

	public PaidPK getId() {
		return this.id;
	}

	public void setId(PaidPK id) {
		this.id = id;
	}

	public Integer getQuantity() {
		return this.quantity;
	}

	public void setQuantity(Integer quantity) {
		this.quantity = quantity;
	}

	public Product getProduct() {
		return this.product;
	}

	public void setProduct(Product product) {
		this.product = product;
	}

	public GiftlistElement getGiftlistElement() {
		return this.giftlistElement;
	}

	public void setGiftlistElement(GiftlistElement giftlistElement) {
		this.giftlistElement = giftlistElement;
	}

	public PaidDTO convertToDTO() {
		PaidDTO paid = new PaidDTO();
		paid.setCartpackageId(this.getId().getCartpackage());
		paid.setUserId(this.getId().getUser());
		paid.setProduct(this.getProduct().convertToDTO());
		paid.setQuantity(this.getQuantity());
		return paid;
	}

}